//为值添加0
function Append_zore(temp) {
  if (temp < 10) {
    return "0" + temp;
  } else {
    return temp;
  }
}
const rulesForm = class {
  arg;
  constructor(arg) {
    this.arg = arg;
  }
  notEmpty() {
    let options = {
      required: true,
      trigger: "blur",
      //用change会触发suggest的格式校验，因为suggest有给字段付初始值的情况
      message: "信息不能为空！"
    };
    options = { ...options, ...this.arg };
    return options;
  }
  phoneNumber() {
    let options = {
      message: "请输入手机号码！",
      formatMsg: "请输入正确手机号码！"
    };
    options = { ...options, ...this.arg };
    return {
      required: false,
      // message: "请输入联系方式！",//和自定义的验证冲突
      trigger: "blur",
      validator: (rule, value, callback) => {
        let vol_ShouJi = /^1\d{10}$/;
        if (value == "" || !value) {
          callback(new Error(options.message));
        } else if (!vol_ShouJi.test(value)) {
          callback(new Error(options.formatMsg));
        } else {
          callback();
        }
      }
    };
  }
  checkCN() {
    //验证是否是中文
    let options = {
      message: "不能为空！",
      formatMsg: "不能输入中文！"
    };
    options = { ...options, ...this.arg };
    return {
      required: false,
      trigger: "blur",
      validator: (rule, value, callback) => {
        let vol_ShouJi = /.*[\u4e00-\u9fa5]+.*$/;
        if (vol_ShouJi.test(value)) {
          callback(new Error(options.formatMsg));
        } else {
          callback();
        }
      }
    };
  }
  checkName() {
    //验证姓名含少数民族姓名
    let options = {
      message: "请输入姓名！",
      formatMsg: "请输入正确姓名！"
    };
    options = { ...options, ...this.arg };
    return {
      required: false,
      trigger: "blur",
      validator: (rule, value, callback) => {
        let vol_name = /^[\u4e00-\u9fa5]{1,6}(·[\u4e00-\u9fa5]{1,6}){0,2}$/;
        if (!value || value == "") {
          callback(new Error(options.message));
        } else if (!vol_name.test(value)) {
          callback(new Error(options.formatMsg));
        } else {
          callback();
        }
      }
    };
  }
  checkIDNumber() {
    let options = {
      message: "请输入身份证号码！",
      formatMsg: "请输入正确身份证号码！"
    };
    options = { ...options, ...this.arg };
    return {
      required: false,
      trigger: "blur",
      validator: (rule, value, callback) => {
        let aCity = {
          11: "北京",
          12: "天津",
          13: "河北",
          14: "山西",
          15: "内蒙古",
          21: "辽宁",
          22: "吉林",
          23: "黑龙江",
          31: "上海",
          32: "江苏",
          33: "浙江",
          34: "安徽",
          35: "福建",
          36: "江西",
          37: "山东",
          41: "河南",
          42: "湖北",
          43: "湖南",
          44: "广东",
          45: "广西",
          46: "海南",
          50: "重庆",
          51: "四川",
          52: "贵州",
          53: "云南",
          54: "西藏",
          61: "陕西",
          62: "甘肃",
          63: "青海",
          64: "宁夏",
          65: "新疆",
          71: "台湾",
          81: "香港",
          82: "澳门",
          91: "国外"
        };
        let person_id = value;
        let sum = 0;
        //出生日期
        let birthday;
        if (person_id) {
          let pattern = new RegExp(/(^\d{15}$)|(^\d{17}(\d|x|X)$)/i);
          if (pattern.exec(person_id)) {
            //验证身份证的合法性的正则
            pattern = new RegExp(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/);
            if (pattern.exec(person_id)) {
              //获取15位证件号中的出生日期并转位正常日期
              birthday = "19" + person_id.substring(6, 8) + "-" + person_id.substring(8, 10) + "-" + person_id.substring(10, 12);
            } else {
              person_id = person_id.replace(/x|X$/i, "a");
              //获取18位证件号中的出生日期
              birthday = person_id.substring(6, 10) + "-" + person_id.substring(10, 12) + "-" + person_id.substring(12, 14);

              //校验18位身份证号码的合法性
              for (let i = 17; i >= 0; i--) {
                sum += (Math.pow(2, i) % 11) * parseInt(person_id.charAt(17 - i), 11);
              }
              if (sum % 11 != 1) {
                let errMsg = "身份证号码不符合国定标准，请核对！";
                callback(new Error(errMsg));
                return;
              } else {
                callback();
              }
            }
            //检测证件地区的合法性
            if (aCity[parseInt(person_id.substring(0, 2))] == null) {
              let errMsg = "身份证证件地区未知，请核对！";
              callback(new Error(errMsg));
              return;
            } else {
              callback();
            }
            let dateStr = new Date(birthday.replace(/-/g, "/"));

            if (
              birthday !=
              dateStr.getFullYear() + "-" + Append_zore(dateStr.getMonth() + 1) + "-" + Append_zore(dateStr.getDate())
            ) {
              let errMsg = "身份证证件出生日期非法";
              callback(new Error(errMsg));
              return;
            } else {
              callback();
            }
          } else {
            let errMsg = "身份证证件号码格式非法！";
            callback(new Error(errMsg));
            return;
          }
        } else {
          callback(new Error(options.message));
        }
      }
    };
  }
  checkNotNegative() {
    //非负数
    return {
      require: false,
      trigger: "blur",
      validator: (rule, value, callback) => {
        let vol_name = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/;
        if (value) {
          if (!vol_name.test(value)) {
            callback(new Error("请输入非负数！"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }
    };
  }
  checkNumber() {
    //只数字，允许小数
    return {
      require: false,
      trigger: "blur",
      validator: (rule, value, callback) => {
        let vol_name = /^-?(\d+)$|^-?(\d+\.\d+)$/;
        if (value) {
          if (!vol_name.test(value)) {
            callback(new Error("请输入数字！"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }
    };
  }
  checkNotNegativeAndAboveZero() {
    //非负数，且大于零
    return {
      require: false,
      trigger: "blur",
      validator: (rule, value, callback) => {
        let vol_name = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/;
        if (value) {
          if (value == 0) {
            callback(new Error("请输入大于0的数字！"));
          } else if (!vol_name.test(value)) {
            callback(new Error("请输入大于0的数字！"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }
    };
  }
  checkInt() {
    //大于0的整数
    return {
      require: false,
      trigger: "blur",
      validator: (rule, value, callback) => {
        let vol_name = /^[1-9]\d*$/;
        if (value) {
          if (value == 0) {
            callback(new Error("请输入大于0的整数！"));
          } else if (!vol_name.test(value)) {
            callback(new Error("请输入大于0的整数！"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }
    };
  }
};
export { rulesForm };
